#coding:utf-8
import urllib
import urllib.request
import re
def getHTML(url):
    page = urllib.request.urlopen(url)
    html = page.read().decode()
    return html
#print(getHTML('https://www.woyaogexing.com/tupian/dongman/'))
def getImg(url):
    #图片的正则表达式   <img src='xxxx.jpg'> <img src='xxxx.png'>
    reg = r'img.*?src="(.*?\.jpg|.*?\.png)"'
    imgre = re.compile(reg) #得到匹配图片的正则规则
    imgList = re.findall(imgre,getHTML(url)) 
    print(imgList)
    x = 0 #图片的下标
    print('start down pic')
    for imgUrl in imgList:
        if not 'https:' in imgUrl:
            imgUrl = 'https:'+imgUrl
        resp = urllib.request.urlopen(imgUrl)
        reqHtml = resp.read() #读出图片所在的页面内容
        if 'jpg' in imgUrl:
            picFile = open(r'E:\\SC\\SC1802\\Workspace\\eclipse\\1771\\day11\\Img\\%s.jpg' %x,'wb')
        elif 'png' in imgUrl:
            picFile = open(r'E:\\SC\\SC1802\\Workspace\\eclipse\\1771\\day11\\Img\\%s.png' %x,'wb')
        picFile.write(reqHtml)
        picFile.close()
        x = x + 1
print(getHTML('https://uav-cn.tmotor.com/'))
getImg('https://uav-cn.tmotor.com/')